﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;


namespace StudentWeb.Api.Controllers
{
    public class BookController : ApiController
    {
        
        BookBO _bookBO = new BookBO();

        #region 学生信息 保存单条记录
        /// <summary>
        /// 学生信息 保存单条记录
        /// </summary>
        /// <param name="token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        //[ValidToken]
        public IHttpActionResult SaveBookInfo([FromBody] JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {

                var result = _bookBO.SaveBookInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 单条记录
        /// <summary>
        /// 书籍信息 单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        //[ValidToken]
        public IHttpActionResult GetBookInfo(int bookID)
        {
            try
            {
                var result = _bookBO.GetBookInfo(bookID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 添加单条记录
        /// <summary>
        /// 书籍信息 单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [ValidToken]
        public IHttpActionResult InsertBookInfo(string token , [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.InsertBookInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 删除单条记录
        /// <summary>
        /// 书籍信息 删除单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]

        public IHttpActionResult DeleteBookInfo([FromBody]JObject obj)
        {
            if (obj == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.DeleteBookInfo(obj["BookID"].ToObject<int>());
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 修改单条记录
        /// <summary>
        /// 书籍信息 修改单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]

        public IHttpActionResult UpdateBookInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.UpdateBookInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 全部记录
        /// <summary>
        /// 书籍信息 全部记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[ValidToken]
        public IHttpActionResult GetBookList()
        {
            try
            {
                var result = _bookBO.GetBookList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
            #endregion



            

        }

    }
}